/*******************************************************************************
* Long-term effects of weather-induced migration on urban labor and housing 
  markets
* Busso & Chauvin

* Purpose: Calculate the descriptive statistics of labor and housing
markets outcomes of resident.

*******************************************************************************/

/*******************************************************************************
*  Changes (1991-2010)
*******************************************************************************/
global vars d_l_rooms_h_r_9110 d_l_houses_h_r_9110 d_l_rentres2_h_r_9110 ///
d_l_rooms_m_r_9110 d_l_houses_m_r_9110 d_l_rentres2_m_r_9110 ///
d_l_rooms_l_r_9110 d_l_houses_l_r_9110 d_l_rentres2_l_r_9110 ///
d_own_residents_9110 d_l_rooms_r_9110 d_l_houses_r_9110 d_l_rentres2_r_9110 ///
d_rate_inform2_r_9110 d_rate_inform_r_9110 d_hs_r_9110 d_imf_sh_r_9110 ///
d_isv_sh_r_9110 d_rate_part_r_9110 d_l_emp_r_9110 d_l_wageres2_r_9110


local contador = 0

foreach x in $vars {
	
	local contador = `contador' + 1

	use "$db_out/shift_share_db_cities.dta", clear

	keep if year == 1991

	keep `x'
	
	collapse (mean) mean = `x' (sd) sd = `x'
	
	gen variable = "`x'"
	
	if `contador' == 1 {
		tempfile descriptive_stats
		save `descriptive_stats', replace
	}
	
	else {
		append using `descriptive_stats'
		save `descriptive_stats', replace	
	}
	
}

gen mean_s = string(mean, "%20.2fc")
gen sd_s = string(sd, "%20.2fc")
drop mean sd

gen n = _n

tempfile cols3_4
save `cols3_4', replace

/*******************************************************************************
* Base-year variables (1991)
*******************************************************************************/
global vars l_rooms_h_r l_houses_h_r l_rentres2_h_r l_rooms_m_r l_houses_m_r ///
l_rentres2_m_r l_rooms_l_r l_houses_l_r l_rentres2_l_r own_residents l_rooms_r ///
l_houses_r l_rentres2_r rate_inform2_r rate_inform_r hs_r imf_sh_r isv_sh_r ///
rate_part_r l_emp_r l_wageres2_r

local contador = 0

foreach x in $vars {
	
	local contador = `contador' + 1

	use "$db_tmp/mcarp8010_analysis_dataset.dta", clear

	keep if year == 1991

	keep `x'
	
	collapse (mean) mean = `x' (sd) sd = `x'
	
	gen variable2 = "`x'"
	
	if `contador' == 1 {
		tempfile descriptive_stats1991
		save `descriptive_stats1991', replace
	}
	
	else {
		append using `descriptive_stats1991'
		save `descriptive_stats1991', replace	
	}
}

gen mean_s2 = string(mean, "%20.2fc")
gen sd_s2 = string(sd, "%20.2fc")
drop mean sd

gen n = _n

merge 1:1 n using `cols3_4'

/*******************************************************************************
* Format table
*******************************************************************************/
drop _merge n variable
rename variable2 variable 

gen variable_name = ""
	replace variable_name = "\cmidrule{1-6} Mean Log Wages" in 1
	replace variable_name = "Log Total Employment" in 2
	replace variable_name = "Labor Force Participation Rate" in 3
	replace variable_name = "Share of Employment in Services" in 4
	replace variable_name = "Share of Employment in Manufacturing" in 5
	replace variable_name = "Share with high school or more" in 6
	replace variable_name = "Informality Rate (including self-employment)" in 7
	replace variable_name = "Informality Rate" in 8
	replace variable_name = "~~~~~Mean Log Housing Rents" in 9
	replace variable_name = "~~~~~Log Total Number of Housing Units" in 10
	replace variable_name = "~~~~~Log Total Number of Rooms" in 11
	replace variable_name = "~~~~~Ownership Rate" in 12
	replace variable_name = "~~~~~Mean Log Housing Rents" in 13
	replace variable_name = "~~~~~Log Total Number of Housing Units" in 14
	replace variable_name = "~~~~~Log Total Number of Rooms" in 15
	replace variable_name = "~~~~~Mean Log Housing Rents" in 16
	replace variable_name = "~~~~~Log Total Number of Housing Units" in 17
	replace variable_name = "~~~~~Log Total Number of Rooms" in 18
	replace variable_name = "~~~~~Mean Log Housing Rents" in 19
	replace variable_name = "~~~~~Log Total Number of Housing Units" in 20
	replace variable_name = "~~~~~Log Total Number of Rooms" in 21


gen aux = ""
drop variable
order variable_name mean_s2 sd_s2 aux

insobs 1, before(1)
insobs 1, before(10)
replace variable_name = "\cmidrule{1-6} \textbf{Panel A: Labor-market Outcomes}" in 1
replace variable_name = "\cmidrule{1-6} \textbf{Panel B: Housing-market Outcomes}" in 10

insobs 1, before(11)
insobs 1, before(16)
insobs 1, before(20)
insobs 1, before(24)
replace variable_name = "\textit{All residents' housing units}" in 11
replace variable_name = "\textit{Penurious housing}" in 16
replace variable_name = "\textit{Low-quality housing}" in 20
replace variable_name = "\textit{Quality housing}" in 24

egen latex = concat(*), p("&")
replace latex = latex + "\\"
keep latex

insobs 3, before(1)
replace latex = "\toprule & \multicolumn{2}{c}{Base-year variables (1991)} && \multicolumn{2}{c}{Changes (1991-2010)} \\" in 1
replace latex = "\cmidrule{2-3} \cmidrule{5-6} & Mean & Std. dev. && Mean & Std. dev. \\" in 2
replace latex = "& (1) & (2) && (3) & (4) \\" in 3

insobs 2, after(30)
replace latex = "\bottomrule & & & & & \\" in 31
replace latex = "\end{tabular}" in 32

insobs 1, before(1)
replace latex = "\begin{tabular}{lccccc}" in 1

cap file close myfile
file open myfile using "$da_out/tables/appendix_table_1.tex", write replace

forv i = 1/`=_N'{
	loc t0 = latex in `i'
	file write myfile `"`t0'"' _n 
} 

file close myfile



